﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

// 目标头像
public class HeadTarget
{
    GameObject _uiRoot;

    Text _name;
    Image _hpValue;

    GameObject _menuRoot; // 头像菜单根节点

    public void Init()
    {
        _uiRoot = UIManager.Instance.AddUI("Prefabs/UI/FightUI/HeadTarget", UILayer.FightUI);

        _name = _uiRoot.FindComponent<Text>("Name");
        _hpValue = _uiRoot.FindComponent<Image>("Hp/Value");

        Button btn = _uiRoot.FindComponent<Button>("bg");
        btn.onClick.AddListener(onHeadClick);

        Hide();
    }

    private void onHeadClick()
    {
        // 动态生成一个按钮
        _menuRoot = UIManager.Instance.AddUI("Prefabs/UI/FightUI/Menu", UILayer.FightUI);

        var btnWatch = _menuRoot.FindComponent<Button>("BtnWatch");
        var btnTeam = _menuRoot.FindComponent<Button>("BtnTeam");

        btnWatch.onClick.AddListener(onWatch);
        btnTeam.onClick.AddListener(onTeam);

        var autoClose = _menuRoot.AddComponent<TouchOutSideEx>();
        autoClose.OutSideCallback = releaseMenu;
    }

    private void onTeam()
    {
        Debug.Log("请求组队");
        releaseMenu();
    }

    private void onWatch()
    {
        Debug.Log("查看装备");
        releaseMenu();
    }

    // 设置血量
    public void SetHp(float oldValue, float newValue, float maxValue)
    {
        _hpValue.transform.localScale = new Vector3(newValue / maxValue,
            _hpValue.transform.localScale.y,
            _hpValue.transform.localScale.z);
    }

    // 关闭菜单
    private void releaseMenu()
    {
        UIManager.Instance.Release(_menuRoot);
    }

    public void Show(string name)
    {
        _name.text = name;
        _uiRoot.SetActive(true);
    }

    public void Hide()
    {
        _uiRoot.SetActive(false);
    }
}
